<?php
namespace Futures\Controller;
use Futures\Model\OptionModel;
/**
 *  自动更新执行的控制器
 */
class UpdateDollarController{

    public function update_index(){
        header("Access-Control-Allow-Origin:*");
        //$option = M('FuturesOption')->where(array('status'=>1))->find();
        //$option['code'] = M('FuturesOption')->where(array('status'=>1,'type'=>3))->getField('code');
        $option = get_dollar_data();  //1是卖价、3是买价、5是开盘价、9是最新价
        header('Content-Type:application/jsonp; charset=utf-8');
        exit(json_encode($option));
    }
    
    public function fenshi(){
        //if(empty($code)) return false;
        // $url = "http://quote.jiaoyidaren.com/quote?code={$code}&_=".time();
        $url = 'http://db2015.wstock.cn/wsDB_API2/TheTimeTrend.php?r_type=0&symbol=PMUDI&u=fivip100&p=yixi888';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_ENCODING, "gzip, deflate, sdch");  
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        if(empty($output)) return false;
        
        $data = nl2br($output);//将分行符"\r\n"转义成HTML的换行符"<br />"
        $datas = explode("<br />",$data);//"<br />"作为分隔切成数组
        //dump($datas);
        $arr = array();
        foreach($datas as $k=>$val){
            //dump($val);
            $vs = explode(',',$val);
            if(!empty($vs[4])){
                $newArr[$k]['ys'] = (float)$vs[4];    //均价
                //$newArr[$k]['Open_Int'] = $vs[6];       //持仓量
                //$newArr[$k]['volume'] = $vs[7];       //总成交量
                //$newArr[$k]['amount'] = $vs[8];       //总成总额
                if($k>0&&$datas[$k-1][4]>=$vs[4]){
                    $newArr[$k]['zs'] = (int)$vs[7];
                    $newArr[$k]['ds'] = 0;
                }else{
                    $newArr[$k]['zs'] = 0;
                    $newArr[$k]['ds'] = (int)$vs[7]; 
                }
            }
           

        }
        //$shishi = get_stock_data();
        //dump($shishi);
        //$datas = $shishi;
        //$datas = explode(',',$shishi);
        //$url = "http://cd.fivip.com/futures.php?s=/GetData/get_stock_data";
        $url = U("GetData/get_dollar_data");
        //dump($url);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        $datas=json_decode($output,true);
        
        $shi['close'] = $datas[7];
        $shi['open'] = $datas['open_price'];
        $shi['hign'] = $datas['high_price'];
        $shi['low'] = $datas['low_price'];
        $arr['tu'] = $newArr;
        $arr['shi'] = $shi;
        //dump($newArr);
        echo json_encode($arr);
        //return $output;
    }

    public function mogo_update(){
        set_time_limit(0);
        //ignore_user_abort();
        $M= new OptionModel('dollarorder');//集合名（表名）
        //更新
        //do{
			
           
            $where['sell_price'] = array('lte',0);
            $where['end_time'] = array('lt',time()+1);
            $where['status'] = 1;
            //$list = $M->where($where)->save($o);
			$list = $M->where($where)->select();
			if(empty($list)){
				return;exit;
			} 
				
			
			$option = M('FuturesOption')->where(array('status'=>1,'type'=>4))->find();
            //$option = get_stock_data($option['code']);  //1是卖价、3是买价、5是开盘价、9是最新价
            $option_datas = get_dollar_data($option['code']);
			if(empty($option_datas['last_price'])) return;
            foreach ($list as $k => $v) {
                // if($v['invest']>=200){
                //     if($v['direction']==1){//看涨
                //         $option_datas['last_price']=$option_datas['last_price']-2;
                //     }else{//看跌
                //         $option_datas['last_price']=$option_datas['last_price']+2;
                //     }
                // }
                if($v['direction']){    //看涨
                    $p= $option_datas['last_price']-$v['buy_price'];
                }else{
                    $p = $v['buy_price']-$option_datas['last_price'];
                }
                if($p<0){
                    $data['profit'] = -$v['invest'];
                }elseif($p==0){
                    $data['profit'] = 0;
                }else{
                    $data['profit'] = $v['invest']*(1+$v['yield_rate']);
                    
                }
                $data['sell_price']=$option_datas['last_price'];
                $data['status']=2;
                $res = $M->where(array('id'=>$v['id'],'user_id'=>$v['user_id'],'status'=>1))->save($data);
                //dump($res);
                if($res && $res['n']>=1 && $p>=0){
                    $openid = M('Follow')->where(array('id'=>$v['user_id']))->getField('openid');
                    if($p>0){
                        $note = "美元订单(".$v['id'].")盈利";
                    }elseif($p==0){
                        $note = "美元订单(".$v['id'].")本金退回";
                        $data['profit'] = $v['invest'];
                    }
                    modify_account($openid,$data['profit'],5,$note);
                }
                //dump($res);
            }
            //usleep(700000);
        //}while(true);
    }

}
